package com.hzlj.position.stat.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import com.fz.common.base.core.JsonUtils;
import com.fz.common.base.dto.ResultDTO;
import com.google.common.collect.Lists;
import com.hzlj.position.locate.common.common.PositionWithHandleDTO;
import com.hzlj.position.stat.common.dto.amtStatJzry.AmtStatJzryPositionWarningDetailQueryParamDTO;
import com.hzlj.position.stat.common.dto.amtStatJzry.AmtStatJzryPositionWarningDetailResultDTO;
import com.hzlj.position.stat.service.AmtStatJzryService;
import com.hzlj.sqjz.config.common.dto.file.FileQueryResultDTO;
import com.hzlj.sqjz.jz.common.dto.common.JzCommonQueryResultDTO;
import com.hzlj.sqjz.jz.service.JzJbxxService;
import com.hzlj.sqjz.writ.common.dto.jzWritExeRecord.JzWritExeRecordAddDTO;
import com.hzlj.sqjz.writ.service.JzWritExeRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 【定位统计-汇总】接口
 *
 * @author lifh
 * @date 2023-12-07 09:50:45
 */
@Slf4j
@RestController
@RequestMapping("/position/stat/amt/stat/jzry")
public class AmtStatJzryController {
    @Resource
    private AmtStatJzryService amtStatJzryService;
    @Resource
    private JzWritExeRecordService jzWritExeRecordService;
    @Resource
    private JzJbxxService jzJbxxService;

    /**
     * 导出信息化核查异常情况明细表(多个文件)
     */
    @GetMapping("/export/xxhhcycqkb")
    public ResultDTO<List<FileQueryResultDTO>> exportPositionWarningDetail(AmtStatJzryPositionWarningDetailQueryParamDTO param) {
        log.info("导出定位统计明细:dto={}", param);
        JzCommonQueryResultDTO jzJbxx = jzJbxxService.getJzCommonById(param.getJzId());
        List<PositionWithHandleDTO> positions = amtStatJzryService.listPositionWarningDetail(param);
        Map<String, List<PositionWithHandleDTO>> positionByDeviceCode
                = positions.stream().collect(Collectors.groupingBy(PositionWithHandleDTO::getDeviceCode));

        List<FileQueryResultDTO> files = Lists.newArrayList();
        positionByDeviceCode.forEach((k, v) -> {
            List<AmtStatJzryPositionWarningDetailResultDTO> list = v.stream().map(e -> {
                AmtStatJzryPositionWarningDetailResultDTO dto = new AmtStatJzryPositionWarningDetailResultDTO();
                dto.setWarningTypeText(e.getWarningTypeText());
                dto.setPosition(e.getPosition());
                dto.setPositionTime(DateUtil.formatDateTime(e.getPositionTime()));
                if (e.getHandleExt() != null) {
                    dto.setHandleReason(e.getHandleExt().getHandleReason());
                    dto.setHandlerName(e.getHandleExt().getHandlerName());
                }
                return dto;
            }).collect(Collectors.toList());

            //设置参数
            Map<String, Object> fillParams = MapUtil.<String, Object>builder()
                    .put("xm", jzJbxx.getXm())
                    .put("deviceCode", k)
                    .put("positions", list)
                    .build();

            FileQueryResultDTO file = jzWritExeRecordService.previewJzWritExeRecord(
                    new JzWritExeRecordAddDTO()
                            .setJzId(jzJbxx.getJzId())
                            .setXm(jzJbxx.getXm())
                            .setWritType("9001")
                            .setInputParams(JsonUtils.obj2json(fillParams))
            );

            files.add(file);
        });
        return new ResultDTO<>(files);
    }
}

